#include <stdio.h>
#include <unistd.h>
#include <sys/io.h>

/* The clock frequency of the i8253/i8254 PIT */
#define PIT_TICK_RATE 1193182ul

void beep(unsigned int val)
{
	unsigned int count = 0;

	if (val > 20 && val < 32767)
	      count = PIT_TICK_RATE / val;

	iopl(3);

	if (count) {
		outb_p(0xB6, 0x43);
		outb_p(count & 0xFF, 0x42);
		outb((count >> 8) & 0xFF, 0x42);
		outb_p(inb_p(0x61) | 3, 0x61);
	} else {
		outb(inb_p(0x61) & 0xFC, 0x61);
	}

	iopl(0);
}

int main(int argc,char * argv[])
{
	unsigned int val;

	if (argc != 2) {
	      printf("Usage:%s value\n    20<value<32767,other value will stop!\n",argv[0]);
	      return 0;
	}

	val = atoi(argv[1]);

	beep(val);
	return 0;
}
